/**
 * 
 */
package org.statewalker.parser.wiki.tokenizer.inline;

import org.statewalker.parser.wiki.tokenizer.base.StreamToken;
import org.statewalker.tokenizer.base.AbstractTokenizer;
import org.statewalker.tokenizer.base.ITokenFactory;
import org.statewalker.tokenizer.stream.ICharStream;
import org.statewalker.tokenizer.stream.ICharStream.IMarker;

public class LinkTokenizer extends AbstractTokenizer<StreamToken> {

    public LinkTokenizer(ITokenFactory<StreamToken> factory) {
        super(factory);
    }

    public StreamToken read(ICharStream stream) {
        StreamToken result = null;
        IMarker marker = stream.markPosition();
        try {
            char ch = stream.getChar();
            if (ch == '[') {
                while (ch != 0 && ch != ']') {
                    stream.incPos();
                    ch = stream.getChar();
                }
                if (ch == ']') {
                    stream.incPos();
                    result = newToken(stream, marker);
                }
            }
            return result;
        } finally {
            marker.close(result == null);
        }
    }

}